<?php
/**
 * Class YiiResize
 *
 * Example
 *  $file = new YiiResize();
 *  $file->height = 100;
 *  $file->destination = Yii::getPathOfAlias('webroot').'/images/article/2';
 *  $file->fileName = 'askar';
 *  $file->src = Yii::getPathOfAlias('webroot').'/images/article/2/avatar.jpg';
 *  $file->quality = 95;
 *  $file->resize();
 */
class YiiResize {

public $fileName;
public $height;//высота тумбы
public $width; //ширина тумбы
public $quality; //качество изображения
public $src; //путь до файла
public $destination; //Путь для сохранения файла
public $rewrite; //удаление оригинала
protected  $_image;
protected $_extension;

    function __construct()
    {

    }

    protected function _createImage($src)
    {
        $file = pathinfo($src);

        switch(mb_strtolower($file['extension']))
        {
            case "jpg":
                $this->_image = imagecreatefromjpeg($src);
                $this->_extension = 'jpg';
                break;
            case "jpeg":
                $this->_image = imagecreatefromjpeg($src);
                $this->_extension = 'jpeg';
                break;
            case "png":
                $this->_image = imagecreatefrompng($src);
                $this->_extension = 'png';
                break;
            case "gif":
                $this->_image = imagecreatefromgif($src);
                $this->_extension = 'gif';
                break;
            default:
                throw new Exception('File is not a valid image.');
        }
        return $this->_image;
    }

    protected function _saveImage($image)
    {
        if(isset($this->quality))
        {
            $this->quality = 100;
        }
        switch($this->_extension)
        {
            case "jpg":
                imagejpeg($image, $this->destination.'/'.mb_strtolower($this->fileName), $this->quality);
                break;
            case "jpeg":
                imagejpeg($image, $this->destination.'/'.mb_strtolower($this->fileName), $this->quality);
                break;
            case "png":
                imagepng($image, $this->destination.'/'.mb_strtolower($this->fileName), $this->quality);
                break;
            case "gif":
                imagegif($image, $this->destination.'/'.mb_strtolower($this->fileName), $this->quality);
                break;
            default :throw new Exception('File is not a valid image.');
        }

    }
    public function resize()
    {
        try{
        $this->_createImage($this->src);

        $image_x = imagesx($this->_image);
        $image_y = imagesy($this->_image);

        $height = $this->height;
        if(!isset($this->width))
        {
            $width =$image_x/($image_y/$height);
        }
        else
        {
            $width = $this->width;
        }

        $image = imagecreatetruecolor($width,$height);
        imagecopyresampled($image,$this->_image,0,0,0,0,$width,$height,$image_x,$image_y);

        if($this->rewrite === true)
        {

            @unlink($this->src);
        }
         $this->_saveImage($image);
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }

}
/* $src = Yii::getPathOfAlias('webroot').'/images/article/2/02.jpg';
        $im = imagecreatefromjpeg($src);
        $image_x = imagesx($im);
        $image_y = imagesy($im);

        $new_height = 100;

            $height = $new_height;
            $width = $image_x/($image_y/$new_height);

        $image = imagecreatetruecolor($width,$height);
        imagecopyresampled($image,$im,0,0,0,0,$width,$height,$image_x,$image_y);
        imagejpeg($image, Yii::getPathOfAlias('webroot').'/images/article/2/asauuuagaks.jpg', 100);
*/